package com.cxf.windex.base.util;

import java.io.IOException;

public class ByteUtil {

    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    public static byte[] byteMergerAll(byte[]... values) {
        int length_byte = 0;
        for (int i = 0; i < values.length; i++) {
            length_byte += values[i].length;
        }
        byte[] all_byte = new byte[length_byte];
        int countLength = 0;
        for (int i = 0; i < values.length; i++) {
            byte[] b = values[i];
            System.arraycopy(b, 0, all_byte, countLength, b.length);
            countLength += b.length;
        }
        return all_byte;
    }

    public static String printHexString(byte[] b) {

        String h = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            h = h + " " + hex.toUpperCase();
        }
        return h;

    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     * 
     * @param c
     *            char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static byte[] byteSub(byte[] byte_1, int begin, int end) {
        byte[] byte_3 = new byte[end - begin];
        System.arraycopy(byte_1, begin, byte_3, 0, end - begin);
        return byte_3;
    }

    /**
     * 鏁村瀷杞崲涓�4浣嶅瓧鑺傛暟缁�
     *
     * @return
     */
    public static byte[] intToByteArray(int number, int size) {

        byte[] result = new byte[size];

        for (int i = 0; i < size; i++) {
            result[i] = (byte) (number >> 8 * (size - 1 - i) & 0xFF);
        }

        return result;
    }

    /**
     * byte[]杞琲nt
     * 
     * @param bytes
     * @return
     */
    public static int byteArrayToInt(byte[] bytes, int size) {

        int intValue = 0;
        for (int i = 0; i < bytes.length; i++) {
            intValue += (bytes[i] & 0xFF) << (8 * (size - 1 - i));
        }
        return intValue;
    }

    /**
     * 鏁村瀷杞崲涓�8浣嶅瓧鑺傛暟缁�
     * 
     * @param intValue
     * @return
     */
    // public static byte[] longToByteArray(long number, int size) {
    //
    // byte[] result = new byte[size];
    //
    // for (int i = 0; i < size; i++) {
    // result[i] = (byte) (number >> 8 * (size - 1 - i) & 0xFF);
    // }
    //
    // return result;
    // }

    /**
     * byte[]杞琹ong
     * 
     * @param bytes
     * @return
     */
    // public static long byteArrayToLong(byte[] bytes, int size) {
    // long value = 0;
    // for (int i = 0; i < bytes.length; i++) {
    // value += (bytes[i] & 0xFF) << (8 * (size - 1 - i));
    // }
    //
    // return value;
    // }

    /**
     * byte[]杞琹ong
     * 
     * @param bytes
     * @return
     */
    public static long byteArrayToLong(byte[] bytes, int size) {
        long value = 0;

        for (int i = bytes.length - 1; i >= 0; i--) {
            value <<= 8;
            value |= (bytes[i] & 0xFF);
        }

        return value;
    }

    /**
     * 鏁村瀷杞崲涓�8浣嶅瓧鑺傛暟缁�
     *
     * @return
     */
    public static byte[] longToByteArray(long number, int size) {

        byte[] result = new byte[size];

        for (int i = 0; i < size; i++) {
            result[i] = (byte) ((number >> (8 * i)) & 0xFF);
        }

        return result;
    }

    /*
     * 16进制字符串转字节数组
     */
    public static byte[] hexString2Bytes(String hex) {

        if ((hex == null) || (hex.equals(""))) {
            return null;
        } else if (hex.length() % 2 != 0) {
            return null;
        } else {
            hex = hex.toUpperCase();
            int len = hex.length() / 2;
            byte[] b = new byte[len];
            char[] hc = hex.toCharArray();
            for (int i = 0; i < len; i++) {
                int p = 2 * i;
                b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p + 1]));
            }
            return b;
        }

    }

    /**
     * 校验和
     * 
     * @param msg
     *            需要计算校验和的byte数组
     * @param length
     *            校验和位数
     * @return 计算出的校验和数组
     */
    public static byte[] sumCheck(byte[] msg, int length) {
        long mSum = 0;
        byte[] mByte = new byte[length];

        /** 逐Byte添加位数和 */
        for (byte byteMsg : msg) {
            long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);
            mSum += mNum;
        }
        /** end of for (byte byteMsg : msg) */

        /** 位数和转化为Byte数组 */
        for (int liv_Count = 0; liv_Count < length; liv_Count++) {
            mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
        }
        /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */

        return mByte;
    }

    public static void main(String[] args) throws IOException {
        String str = "ECBC02302A2A0114110B140B2F362A3C012A2A2A2A9BD414110B140B2F102A2A2A3A75110B140B2F2A102A2A5D14110B140B2F02102A2A2A2A9DD414110B140B2E3A102A2A2A14110B140B2F2A2A2A9FD314110B140B2F02102A2A2A14110B140B2F05102A3A75110B140B2F06102A2A2A2AA15B14110B140B2F07102AA2D414110B140B2F09102A2A2A2AA3D414110B140B2F0B102AA4D414110B140B2F0D0A102A2A2A2AA5D414110B140B2F10102AA6D414110B140B2F12102A2A2A3A75110B140B2F12102A2A5C14110B140B2F14102A2A2A14110B140B2F16102A2A2A14110B140B2F18102A2A2AAAD314110B140B2F1A102A2A2A14110B140B2F1D102A3A75110B140B2F1E102A2A2A2AAC5C14110B140B2F1F102AADD414110B140B2F21102A2A2A2AAED414110B140B2F1D102AB9D414110B140B2F1F102A2A2A2AAF8314110B140B2F1F102A2A110B140B2F22100D0A2A2A2A2AB1D414110B140B2F24102A75110B140B2F24102A2A2AB25C14110B140B2F26102A2A2A14110B140B2F28102A2AB4D414110B140B2F2A102A2A2A14110B140B2F2C102AB6D414110B140B2F2F102A2A2A2AB7D314110B140B2F31102AB8D414110B140B2F33102A2A2A2AB9D414110B140B2F35102ABAD414110B140B2F37102A2A2A2A14110B140B2F39C968";
        byte[] array = ByteUtil.hexString2Bytes(str.toUpperCase());
        System.out.println(array.length);
    }
}
